package com.weddingee.action.auth;

import java.util.List;

import com.weddingee.action.base.BaseAction;
import com.weddingee.dao.user.UserDao;
import com.weddingee.model.User;
import com.weddingee.util.Constants;
import com.weddingee.util.DaoFactory;
import com.weddingee.util.cond.UserCondition;
import com.weddingee.dto.ViewDto;

public class AuthAction extends BaseAction{
	private UserDao userDao = (UserDao)DaoFactory.create(6);
	private String account;
	private String password;
	private String statusCode;
	private String errMsg;
	private String typeStr;
	private List<User> userList;
	private ViewDto view = new ViewDto();
	public ViewDto getView() {
		return view;
	}

	public void setView(ViewDto view) {
		this.view = view;
	}

	public String loginHome(){
		return SUCCESS;
	}
	
	public String login(){
		if( null == account || "".equals(account) || null == password || "".equals(password)){
			log.info("参数不正确。");
			errMsg = "请正确填写用户名和密码。";
			return INPUT;
		}
		
		//是否账户存在
		User user = userDao.findByUsername(account);
		typeStr = user.getType();
		UserCondition cond = new UserCondition();
		cond.setPage(1);
		cond.setCount(54);
		cond.setType(typeStr);
		userList = userDao.listUser(cond);
		if( null == user){
			//用户不存在
			log.info("用户不存在。");
			errMsg = "用户名或密码错误";
			return INPUT;
		}
		
		//用户存在
		if( user.getPassword().equals(password) ){
			log.info("登录成功。");
			session.put(Constants.SESSION_KEY_LOGIN_USER,user);
			return SUCCESS;
		}
		else{
			log.info("用户名或密码错误。");
			errMsg = "用户名或密码错误";
			return INPUT;
		}
	}
	
//checkUser Exist or not
	  public String checkUser(){
		  User user = userDao.findByUsername(account);
		  view.setData(user);
		  view.setMsg("SUCCESS");
		  System.out.println(user);
		  return SUCCESS;
	  }
	
	public String regHome(){
		return SUCCESS;
	}
	
	public String reg(){
		
		return SUCCESS;
	}

	/*setter and getter */
	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(String statusCode) {
		this.statusCode = statusCode;
	}

	public String getErrMsg() {
		return errMsg;
	}

	public String getTypeStr() {
		return typeStr;
	}

	public void setTypeStr(String typeStr) {
		this.typeStr = typeStr;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
	
	
	
}
